import {useAccountStore} from "./AccountStore.ts";
import {defineStore} from "pinia";
import type {TopicInfo, TopicListData} from "../network/types/Topic.ts";
import {SearchContent, type TopicOrderBy} from "../network/api/TopicApi.ts";
import {CacheMap, useCache} from "../utils/CacheUtils.ts";
import {useUserStore} from "./UserStore.ts";

const api = () => useAccountStore().getNgaApi().topic


// 检查主题是否应被屏蔽
const checkBlocked = (topic: TopicInfo) => {
    const userStore = useUserStore();
    const {keywords, users} = userStore.blockConfig
    const hasKeyword = keywords.some(it => topic.title?.includes(it))
    const hasUser = users.some(it => topic.authorUid == it.userId)
    topic.blocked = hasKeyword || hasUser
}

export const useTopicStore = defineStore('TopicStore', {
    state: () => ({
        cache: new CacheMap<TopicListData>()
    }),
    actions: {
        async forumList(fid: number,
                        page?: number,
                        orderBy?: TopicOrderBy,
                        force?: boolean,
        ): Promise<TopicListData> {
            const key = `forumList ${fid} ${page} ${orderBy}`
            return useCache(this.cache, key, force || false, () => api().forumList(fid, page, orderBy)).then(res => {
                res.data.forEach(topic => checkBlocked(topic))
                return res
            })
        },
        forumRecommend(fid: number,
                       page?: number,
                       force?: boolean,
        ): Promise<TopicListData> {
            const key = `forumRecommend ${fid} ${page}`
            return useCache(this.cache, key, force || false, () => api().forumRecommend(fid, page))
        },
        forumSearch(keyword: string, fid: string, page?: number, content?: SearchContent, recommend?: 1 | 0, force?: boolean,
        ): Promise<TopicListData> {
            const key = `forumSearch ${keyword} ${fid} ${page} ${content} ${recommend}`
            return useCache(this.cache, key, force || false, () => api().forumSearch(keyword,
                fid.split(',').map(Number),
                page,
                content,
                recommend || 0))
        },
        async colList(colId: number,
                      page?: number,
                      orderBy?: TopicOrderBy,
                      force?: boolean,
        ): Promise<TopicListData> {
            const key = `colList ${colId} ${page} ${orderBy}`
            return useCache(this.cache, key, force || false, () => api().colList(colId, page, orderBy)).then(res => {
                res.data.forEach(topic => checkBlocked(topic))
                return res
            })
        },
        colSearch(keyword: string,
                  colId: number,
                  page?: number,
                  content?: SearchContent,
                  recommend?: 1 | 0,
                  force?: boolean,
        ): Promise<TopicListData> {
            const key = `colSearch ${keyword} ${colId} ${page} ${content} ${recommend}`
            return useCache(this.cache, key, force || false, () => api().colSearch(keyword,
                colId,
                page,
                content,
                recommend || 0))
        },

        userTopic(userId: number,
                  fid?: number | undefined,
                  recommend?: 1 | 0 | undefined,
                  page?: number,
                  force?: boolean,
        ): Promise<TopicListData> {
            const key = `userTopic ${userId} ${fid} ${recommend} ${page}`
            return useCache(this.cache, key, force || false, () => api().userTopic(userId, fid, recommend, page))
        },

        userReplies(userId: number,
                    fid?: number | undefined,
                    page?: number,
                    force?: boolean,
        ): Promise<TopicListData> {
            const key = `userReplies ${userId} ${fid} ${page}`
            return useCache(this.cache, key, force || false, () => api().userReplies(userId, fid, page))
        },
        // 查询收藏夹内的主题
        favorTopic(folderId: number, page?: number | undefined, force?: boolean) {
            const key = `favorTopic ${folderId} ${page}`
            return useCache(this.cache, key, force || false, () => api().favorTopic(folderId, page))
        },

        privilege(fid: number) {
            return api().privilege(fid).then((res: string) => {
                // 替换用户中心链接为本地路由地址
                return res.replace(/\/nuke\.php\?func=ucp&uid=(\d+)/g, "/user/$1")
            })
        }
    },
})